﻿Imports System.IO

<Serializable()> _
Public Class ZuneImage

    Public UUID As String
    Public artistUUID As String
    Public URI As String
    Public isLocal As Boolean
    Public type As ZuneImageType

    Private _tmpImage As Image

    Public Sub New()
        type = ZuneImageType.Gallery
    End Sub

    Public ReadOnly Property TempImage() As Image
        Get
            If _tmpImage Is Nothing Then
                If isLocal Then
                    If IO.File.Exists(URI) Then
                        Dim fs As New FileStream(URI, FileMode.Open, FileAccess.Read)
                        _tmpImage = Image.FromStream(fs)
                        fs.Close()
                    Else
                        _tmpImage = My.Resources._error
                    End If
                Else
                    Dim client As New Net.WebClient
                    Dim resp As IO.Stream = ZuneHelpers.IsURIOnline(URI)

                    If Not File.Exists(DIR_TEMP + Me.UUID + ".img") Then
                        If resp IsNot Nothing Then
                            Try
                                _tmpImage = Image.FromStream(resp)
                                resp.Close()

                                client.DownloadFile(New System.Uri(URI), DIR_TEMP + Me.UUID + ".img")
                            Catch e As Exception
                                _tmpImage = My.Resources._error
                            End Try
                        Else
                            _tmpImage = My.Resources._error
                        End If
                    Else
                        _tmpImage = Image.FromFile(DIR_TEMP + Me.UUID + ".img")
                    End If

                End If
            End If

            Return _tmpImage
        End Get
    End Property

    Public Sub UpdateTemp()
        _tmpImage = Nothing
    End Sub

End Class

Public Enum ZuneImageType
    Background
    DeviceBackground
    Gallery
End Enum
